from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO
from bsc_operations import *
import json

app = Flask(__name__)
socketio = SocketIO(app)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/api/generate_wallets', methods=['POST'])
def api_generate_wallets():
    count = int(request.form.get('count', 10))
    wallets = generate_wallets(count)
    return jsonify({'success': True, 'wallets': wallets})

@app.route('/api/transfer_bnb', methods=['POST'])
def api_transfer_bnb():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    amount = float(request.form.get('amount', 0.001))
    transfer_bnb(wallets, amount)
    return jsonify({'success': True})

@app.route('/api/transfer_cake', methods=['POST'])
def api_transfer_cake():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    amount = float(request.form.get('amount', 100))
    transfer_cake(wallets, amount)
    return jsonify({'success': True})

@app.route('/api/approve_syrup_pool', methods=['POST'])
def api_approve_syrup_pool():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    approve_syrup_pool(wallets)
    return jsonify({'success': True})

@app.route('/api/stake_cake', methods=['POST'])
def api_stake_cake():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    amount = float(request.form.get('amount', 100))
    stake_cake(wallets, amount)
    return jsonify({'success': True})

@app.route('/api/harvest_bgsc', methods=['POST'])
def api_harvest_bgsc():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    harvest_bgsc(wallets)
    return jsonify({'success': True})

@app.route('/api/unstake_cake', methods=['POST'])
def api_unstake_cake():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    amount = float(request.form.get('amount', 100))
    unstake_cake(wallets, amount)
    return jsonify({'success': True})

@app.route('/api/send_cake_to_main', methods=['POST'])
def api_send_cake_to_main():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    amount = float(request.form.get('amount', 100))
    send_cake_to_main(wallets, amount)
    return jsonify({'success': True})

@app.route('/api/send_bgsc_to_main', methods=['POST'])
def api_send_bgsc_to_main():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    send_bgsc_to_main(wallets)
    return jsonify({'success': True})

@app.route('/api/send_bnb_to_main', methods=['POST'])
def api_send_bnb_to_main():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    send_bnb_to_main(wallets)
    return jsonify({'success': True})

@app.route('/api/get_balance', methods=['GET'])
def api_get_balance():
    balance = get_main_wallet_balance()
    return jsonify({'balance': float(balance)})

@app.route('/api/get_all_balances', methods=['GET'])
def api_get_all_balances():
    with open('wallets.txt', 'r') as f:
        wallets = json.load(f)
    
    balances = []
    # total = len(wallets)
    index = 0
    for wallet in wallets:
        # 获取BNB余额
        bnb_balance = w3.eth.get_balance(wallet['address'])
        
        # 获取CAKE余额
        cake_balance = get_token_balance(CAKE_TOKEN_ADDRESS, wallet['address'])
        
        # 获取BGSC余额
        bgsc_balance = get_token_balance(BGSC_TOKEN_ADDRESS, wallet['address'])
        
        balances.append({
            'address': wallet['address'],
            'bnb': float(w3.from_wei(bnb_balance, 'ether')),
            'cake': float(cake_balance),
            'bgsc': float(bgsc_balance)
        })

        print(f"{index}------wallet:{wallet['address']}\n"
              f"cake:{float(cake_balance)}\n"
              f"bgsc:{float(bgsc_balance)}\n")
        index = index + 1
        time.sleep(1)
    
    # 获取主钱包余额
    main_bnb = get_main_wallet_balance()
    main_cake = get_token_balance(CAKE_TOKEN_ADDRESS, MAIN_WALLET_ADDRESS)
    main_bgsc = get_token_balance(BGSC_TOKEN_ADDRESS, MAIN_WALLET_ADDRESS)
    
    balances.append({
        'address': MAIN_WALLET_ADDRESS,
        'bnb': float(main_bnb),
        'cake': float(main_cake),
        'bgsc': float(main_bgsc),
        'is_main': True
    })
    
    return jsonify({'balances': balances})

if __name__ == '__main__':
    socketio.run(app, debug=True)